package com.wjy.Data_Structure.stack;

import com.wjy.Data_Structure.linearlist.common.SLNode;

//Stack 的链式存储实现 
public class StackSLinked implements Stack {
	private SLNode top; // 链表首结点引用
	private int size; // 栈的大小

	public StackSLinked() {
		top = null;
		size = 0;
	}

	@Override
	public int getSize() {
		return size;
	}

	@Override
	public boolean isEmpty() {
		return size == 0;
	}

	@Override
	public void push(Object e) {
		SLNode q = new SLNode(e, top);
		top = q;
		size++;

	}

	@Override
	public Object pop() throws StackEmptyException {
		if (size < 1)
			throw new StackEmptyException("错误，堆栈为空。");
		Object obj = top.getData();
		top = top.getNext();
		size--;
		return obj;
	}

	@Override
	public Object peek() throws StackEmptyException {
		if (size < 1)
			throw new StackEmptyException("错误，堆栈为空。");
		return top.getData();
	}

}
